/**
 * @(#)BotProxy.java, 5月 22, 2024.
 * <p>
 * Copyright 2024 yuanfudao.com. All rights reserved.
 * FENBI.COM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.lenovo.agent.proxy;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.WebClient;

import java.io.Serializable;
import java.time.Duration;
import java.util.Map;

/**
 * @author 
 */
@Slf4j
public class HttpProxy {

    @Autowired
    protected WebClient.Builder webClientBuilder;

    protected String postBody(String url, Serializable body) {
        return postBody(url, body, 10L);
    }

    protected String postBody(String url, Serializable body, long timeoutSeconds) {
        try {
            log.info("post url: {}, body: {}", url, body);
            return webClientBuilder
                    .build()
                    .post()
                    .uri(url)
                    .body(BodyInserters.fromValue(body))
                    .retrieve()
                    .bodyToMono(String.class)
                    .timeout(Duration.ofSeconds(timeoutSeconds))
                    .block();
        } catch (Exception e) {
            log.error("postBody url: {}, body: {}, exception:{}", url, body, e.getMessage());
            throw new RuntimeException(e);
        }
    }

    protected String postBodyWithHeader(String url, String header, Serializable body, long timeoutSeconds) {
        try {
            log.info("post url: {}, body: {}", url, body);
            return webClientBuilder
                    .defaultHeader(HttpHeaders.AUTHORIZATION, header)
                    .build()
                    .post()
                    .uri(url)
                    .body(BodyInserters.fromValue(body))
                    .retrieve()
                    .bodyToMono(String.class)
                    .timeout(Duration.ofSeconds(timeoutSeconds))
                    .block();
        } catch (Exception e) {
            log.error("postBody url: {}, body: {}, exception:{}", url, body, e.getMessage());
            throw new RuntimeException(e);
        }
    }

    protected String postParam(String url, Map<String, String> params) {
        return postParam(url, params, 10L);
    }

    protected String postParam(String url, Map<String, String> params, long timeoutSeconds) {
        try {
            log.info("post url: {}, params: {}", url, params);

            MultiValueMap<String, String> data = new LinkedMultiValueMap<>();
            data.setAll(params);

            return webClientBuilder.build()
                    .post()
                    .uri(url)
                    .body(BodyInserters.fromFormData(data))
                    .retrieve()
                    .bodyToMono(String.class)
                    .timeout(Duration.ofSeconds(timeoutSeconds))
                    .block();
        } catch (Exception e) {
            log.error("postParam url: {}, param: {}, exception:{}", url, params, e.getMessage());
            throw new RuntimeException(e);
        }
    }

}